﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace YY.DAL
{
	/// <summary>
	/// 数据访问类:UserInfor
	/// </summary>
	public partial class UserInfor
	{
		public UserInfor()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(Guid UId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from tb_UserInfor");
			strSql.Append(" where UId=@UId ");
			SqlParameter[] parameters = {
					new SqlParameter("@UId", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = UId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(YY.Model.UserInfor model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into tb_UserInfor(");
			strSql.Append("UId,UserName,TrueName,IDNumber,PayPassword,Password,Gender,Age,PhoneNumberOne,PhoneNumberTwo,TelePhoneNumber,Email,MailActivation,QQ,QQTakenId,ResisterTime,HeadImgUrl,ContactAddress,IDAddress,GlyphPassword,GlyphPayPassword,IsDelete,DeleteTime,DeleteUserId,HaveFinance,Height,Weight,ChronicDisease,HereditaryDisease,ChronicDiseaseOther,HereditaryDiseaseOther,CId)");
			strSql.Append(" values (");
			strSql.Append("@UId,@UserName,@TrueName,@IDNumber,@PayPassword,@Password,@Gender,@Age,@PhoneNumberOne,@PhoneNumberTwo,@TelePhoneNumber,@Email,@MailActivation,@QQ,@QQTakenId,@ResisterTime,@HeadImgUrl,@ContactAddress,@IDAddress,@GlyphPassword,@GlyphPayPassword,@IsDelete,@DeleteTime,@DeleteUserId,@HaveFinance,@Height,@Weight,@ChronicDisease,@HereditaryDisease,@ChronicDiseaseOther,@HereditaryDiseaseOther,@CId)");
			SqlParameter[] parameters = {
					new SqlParameter("@UId", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@TrueName", SqlDbType.NVarChar,50),
					new SqlParameter("@IDNumber", SqlDbType.NVarChar,20),
					new SqlParameter("@PayPassword", SqlDbType.NVarChar,50),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@Gender", SqlDbType.Int,4),
					new SqlParameter("@Age", SqlDbType.DateTime),
					new SqlParameter("@PhoneNumberOne", SqlDbType.NVarChar,11),
					new SqlParameter("@PhoneNumberTwo", SqlDbType.NVarChar,11),
					new SqlParameter("@TelePhoneNumber", SqlDbType.NVarChar,20),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@MailActivation", SqlDbType.Int,4),
					new SqlParameter("@QQ", SqlDbType.NVarChar,11),
					new SqlParameter("@QQTakenId", SqlDbType.NVarChar,50),
					new SqlParameter("@ResisterTime", SqlDbType.DateTime),
					new SqlParameter("@HeadImgUrl", SqlDbType.NVarChar,500),
					new SqlParameter("@ContactAddress", SqlDbType.NVarChar,200),
					new SqlParameter("@IDAddress", SqlDbType.NVarChar,200),
					new SqlParameter("@GlyphPassword", SqlDbType.Text),
					new SqlParameter("@GlyphPayPassword", SqlDbType.Text),
					new SqlParameter("@IsDelete", SqlDbType.Int,4),
					new SqlParameter("@DeleteTime", SqlDbType.DateTime),
					new SqlParameter("@DeleteUserId", SqlDbType.NVarChar,50),
					new SqlParameter("@HaveFinance", SqlDbType.Int,4),
					new SqlParameter("@Height", SqlDbType.VarChar,50),
					new SqlParameter("@Weight", SqlDbType.VarChar,50),
					new SqlParameter("@ChronicDisease", SqlDbType.VarChar,500),
					new SqlParameter("@HereditaryDisease", SqlDbType.VarChar,500),
					new SqlParameter("@ChronicDiseaseOther", SqlDbType.NVarChar,500),
					new SqlParameter("@HereditaryDiseaseOther", SqlDbType.NVarChar,500),
					new SqlParameter("@CId", SqlDbType.UniqueIdentifier,16)};
			parameters[0].Value = Guid.NewGuid();
			parameters[1].Value = model.UserName;
			parameters[2].Value = model.TrueName;
			parameters[3].Value = model.IDNumber;
			parameters[4].Value = model.PayPassword;
			parameters[5].Value = model.Password;
			parameters[6].Value = model.Gender;
			parameters[7].Value = model.Age;
			parameters[8].Value = model.PhoneNumberOne;
			parameters[9].Value = model.PhoneNumberTwo;
			parameters[10].Value = model.TelePhoneNumber;
			parameters[11].Value = model.Email;
			parameters[12].Value = model.MailActivation;
			parameters[13].Value = model.QQ;
			parameters[14].Value = model.QQTakenId;
			parameters[15].Value = model.ResisterTime;
			parameters[16].Value = model.HeadImgUrl;
			parameters[17].Value = model.ContactAddress;
			parameters[18].Value = model.IDAddress;
			parameters[19].Value = model.GlyphPassword;
			parameters[20].Value = model.GlyphPayPassword;
			parameters[21].Value = model.IsDelete;
			parameters[22].Value = model.DeleteTime;
			parameters[23].Value = model.DeleteUserId;
			parameters[24].Value = model.HaveFinance;
			parameters[25].Value = model.Height;
			parameters[26].Value = model.Weight;
			parameters[27].Value = model.ChronicDisease;
			parameters[28].Value = model.HereditaryDisease;
			parameters[29].Value = model.ChronicDiseaseOther;
			parameters[30].Value = model.HereditaryDiseaseOther;
			parameters[31].Value = Guid.NewGuid();

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(YY.Model.UserInfor model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tb_UserInfor set ");
			strSql.Append("UserName=@UserName,");
			strSql.Append("TrueName=@TrueName,");
			strSql.Append("IDNumber=@IDNumber,");
			strSql.Append("PayPassword=@PayPassword,");
			strSql.Append("Password=@Password,");
			strSql.Append("Gender=@Gender,");
			strSql.Append("Age=@Age,");
			strSql.Append("PhoneNumberOne=@PhoneNumberOne,");
			strSql.Append("PhoneNumberTwo=@PhoneNumberTwo,");
			strSql.Append("TelePhoneNumber=@TelePhoneNumber,");
			strSql.Append("Email=@Email,");
			strSql.Append("MailActivation=@MailActivation,");
			strSql.Append("QQ=@QQ,");
			strSql.Append("QQTakenId=@QQTakenId,");
			strSql.Append("ResisterTime=@ResisterTime,");
			strSql.Append("HeadImgUrl=@HeadImgUrl,");
			strSql.Append("ContactAddress=@ContactAddress,");
			strSql.Append("IDAddress=@IDAddress,");
			strSql.Append("GlyphPassword=@GlyphPassword,");
			strSql.Append("GlyphPayPassword=@GlyphPayPassword,");
			strSql.Append("IsDelete=@IsDelete,");
			strSql.Append("DeleteTime=@DeleteTime,");
			strSql.Append("DeleteUserId=@DeleteUserId,");
			strSql.Append("HaveFinance=@HaveFinance,");
			strSql.Append("Height=@Height,");
			strSql.Append("Weight=@Weight,");
			strSql.Append("ChronicDisease=@ChronicDisease,");
			strSql.Append("HereditaryDisease=@HereditaryDisease,");
			strSql.Append("ChronicDiseaseOther=@ChronicDiseaseOther,");
			strSql.Append("HereditaryDiseaseOther=@HereditaryDiseaseOther,");
			strSql.Append("CId=@CId");
			strSql.Append(" where UId=@UId ");
			SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@TrueName", SqlDbType.NVarChar,50),
					new SqlParameter("@IDNumber", SqlDbType.NVarChar,20),
					new SqlParameter("@PayPassword", SqlDbType.NVarChar,50),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@Gender", SqlDbType.Int,4),
					new SqlParameter("@Age", SqlDbType.DateTime),
					new SqlParameter("@PhoneNumberOne", SqlDbType.NVarChar,11),
					new SqlParameter("@PhoneNumberTwo", SqlDbType.NVarChar,11),
					new SqlParameter("@TelePhoneNumber", SqlDbType.NVarChar,20),
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@MailActivation", SqlDbType.Int,4),
					new SqlParameter("@QQ", SqlDbType.NVarChar,11),
					new SqlParameter("@QQTakenId", SqlDbType.NVarChar,50),
					new SqlParameter("@ResisterTime", SqlDbType.DateTime),
					new SqlParameter("@HeadImgUrl", SqlDbType.NVarChar,500),
					new SqlParameter("@ContactAddress", SqlDbType.NVarChar,200),
					new SqlParameter("@IDAddress", SqlDbType.NVarChar,200),
					new SqlParameter("@GlyphPassword", SqlDbType.Text),
					new SqlParameter("@GlyphPayPassword", SqlDbType.Text),
					new SqlParameter("@IsDelete", SqlDbType.Int,4),
					new SqlParameter("@DeleteTime", SqlDbType.DateTime),
					new SqlParameter("@DeleteUserId", SqlDbType.NVarChar,50),
					new SqlParameter("@HaveFinance", SqlDbType.Int,4),
					new SqlParameter("@Height", SqlDbType.VarChar,50),
					new SqlParameter("@Weight", SqlDbType.VarChar,50),
					new SqlParameter("@ChronicDisease", SqlDbType.VarChar,500),
					new SqlParameter("@HereditaryDisease", SqlDbType.VarChar,500),
					new SqlParameter("@ChronicDiseaseOther", SqlDbType.NVarChar,500),
					new SqlParameter("@HereditaryDiseaseOther", SqlDbType.NVarChar,500),
					new SqlParameter("@CId", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@UId", SqlDbType.UniqueIdentifier,16)};
			parameters[0].Value = model.UserName;
			parameters[1].Value = model.TrueName;
			parameters[2].Value = model.IDNumber;
			parameters[3].Value = model.PayPassword;
			parameters[4].Value = model.Password;
			parameters[5].Value = model.Gender;
			parameters[6].Value = model.Age;
			parameters[7].Value = model.PhoneNumberOne;
			parameters[8].Value = model.PhoneNumberTwo;
			parameters[9].Value = model.TelePhoneNumber;
			parameters[10].Value = model.Email;
			parameters[11].Value = model.MailActivation;
			parameters[12].Value = model.QQ;
			parameters[13].Value = model.QQTakenId;
			parameters[14].Value = model.ResisterTime;
			parameters[15].Value = model.HeadImgUrl;
			parameters[16].Value = model.ContactAddress;
			parameters[17].Value = model.IDAddress;
			parameters[18].Value = model.GlyphPassword;
			parameters[19].Value = model.GlyphPayPassword;
			parameters[20].Value = model.IsDelete;
			parameters[21].Value = model.DeleteTime;
			parameters[22].Value = model.DeleteUserId;
			parameters[23].Value = model.HaveFinance;
			parameters[24].Value = model.Height;
			parameters[25].Value = model.Weight;
			parameters[26].Value = model.ChronicDisease;
			parameters[27].Value = model.HereditaryDisease;
			parameters[28].Value = model.ChronicDiseaseOther;
			parameters[29].Value = model.HereditaryDiseaseOther;
			parameters[30].Value = model.CId;
			parameters[31].Value = model.UId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid UId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_UserInfor ");
			strSql.Append(" where UId=@UId ");
			SqlParameter[] parameters = {
					new SqlParameter("@UId", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = UId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string UIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_UserInfor ");
			strSql.Append(" where UId in ("+UIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public YY.Model.UserInfor GetModel(Guid UId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 UId,UserName,TrueName,IDNumber,PayPassword,Password,Gender,Age,PhoneNumberOne,PhoneNumberTwo,TelePhoneNumber,Email,MailActivation,QQ,QQTakenId,ResisterTime,HeadImgUrl,ContactAddress,IDAddress,GlyphPassword,GlyphPayPassword,IsDelete,DeleteTime,DeleteUserId,HaveFinance,Height,Weight,ChronicDisease,HereditaryDisease,ChronicDiseaseOther,HereditaryDiseaseOther,CId from tb_UserInfor ");
			strSql.Append(" where UId=@UId ");
			SqlParameter[] parameters = {
					new SqlParameter("@UId", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = UId;

			YY.Model.UserInfor model=new YY.Model.UserInfor();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public YY.Model.UserInfor DataRowToModel(DataRow row)
		{
			YY.Model.UserInfor model=new YY.Model.UserInfor();
			if (row != null)
			{
				if(row["UId"]!=null && row["UId"].ToString()!="")
				{
					model.UId= new Guid(row["UId"].ToString());
				}
				if(row["UserName"]!=null)
				{
					model.UserName=row["UserName"].ToString();
				}
				if(row["TrueName"]!=null)
				{
					model.TrueName=row["TrueName"].ToString();
				}
				if(row["IDNumber"]!=null)
				{
					model.IDNumber=row["IDNumber"].ToString();
				}
				if(row["PayPassword"]!=null)
				{
					model.PayPassword=row["PayPassword"].ToString();
				}
				if(row["Password"]!=null)
				{
					model.Password=row["Password"].ToString();
				}
				if(row["Gender"]!=null && row["Gender"].ToString()!="")
				{
					model.Gender=int.Parse(row["Gender"].ToString());
				}
				if(row["Age"]!=null && row["Age"].ToString()!="")
				{
					model.Age=DateTime.Parse(row["Age"].ToString());
				}
				if(row["PhoneNumberOne"]!=null)
				{
					model.PhoneNumberOne=row["PhoneNumberOne"].ToString();
				}
				if(row["PhoneNumberTwo"]!=null)
				{
					model.PhoneNumberTwo=row["PhoneNumberTwo"].ToString();
				}
				if(row["TelePhoneNumber"]!=null)
				{
					model.TelePhoneNumber=row["TelePhoneNumber"].ToString();
				}
				if(row["Email"]!=null)
				{
					model.Email=row["Email"].ToString();
				}
				if(row["MailActivation"]!=null && row["MailActivation"].ToString()!="")
				{
					model.MailActivation=int.Parse(row["MailActivation"].ToString());
				}
				if(row["QQ"]!=null)
				{
					model.QQ=row["QQ"].ToString();
				}
				if(row["QQTakenId"]!=null)
				{
					model.QQTakenId=row["QQTakenId"].ToString();
				}
				if(row["ResisterTime"]!=null && row["ResisterTime"].ToString()!="")
				{
					model.ResisterTime=DateTime.Parse(row["ResisterTime"].ToString());
				}
				if(row["HeadImgUrl"]!=null)
				{
					model.HeadImgUrl=row["HeadImgUrl"].ToString();
				}
				if(row["ContactAddress"]!=null)
				{
					model.ContactAddress=row["ContactAddress"].ToString();
				}
				if(row["IDAddress"]!=null)
				{
					model.IDAddress=row["IDAddress"].ToString();
				}
				if(row["GlyphPassword"]!=null)
				{
					model.GlyphPassword=row["GlyphPassword"].ToString();
				}
				if(row["GlyphPayPassword"]!=null)
				{
					model.GlyphPayPassword=row["GlyphPayPassword"].ToString();
				}
				if(row["IsDelete"]!=null && row["IsDelete"].ToString()!="")
				{
					model.IsDelete=int.Parse(row["IsDelete"].ToString());
				}
				if(row["DeleteTime"]!=null && row["DeleteTime"].ToString()!="")
				{
					model.DeleteTime=DateTime.Parse(row["DeleteTime"].ToString());
				}
				if(row["DeleteUserId"]!=null)
				{
					model.DeleteUserId=row["DeleteUserId"].ToString();
				}
				if(row["HaveFinance"]!=null && row["HaveFinance"].ToString()!="")
				{
					model.HaveFinance=int.Parse(row["HaveFinance"].ToString());
				}
				if(row["Height"]!=null)
				{
					model.Height=row["Height"].ToString();
				}
				if(row["Weight"]!=null)
				{
					model.Weight=row["Weight"].ToString();
				}
				if(row["ChronicDisease"]!=null)
				{
					model.ChronicDisease=row["ChronicDisease"].ToString();
				}
				if(row["HereditaryDisease"]!=null)
				{
					model.HereditaryDisease=row["HereditaryDisease"].ToString();
				}
				if(row["ChronicDiseaseOther"]!=null)
				{
					model.ChronicDiseaseOther=row["ChronicDiseaseOther"].ToString();
				}
				if(row["HereditaryDiseaseOther"]!=null)
				{
					model.HereditaryDiseaseOther=row["HereditaryDiseaseOther"].ToString();
				}
				if(row["CId"]!=null && row["CId"].ToString()!="")
				{
					model.CId= new Guid(row["CId"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UId,UserName,TrueName,IDNumber,PayPassword,Password,Gender,Age,PhoneNumberOne,PhoneNumberTwo,TelePhoneNumber,Email,MailActivation,QQ,QQTakenId,ResisterTime,HeadImgUrl,ContactAddress,IDAddress,GlyphPassword,GlyphPayPassword,IsDelete,DeleteTime,DeleteUserId,HaveFinance,Height,Weight,ChronicDisease,HereditaryDisease,ChronicDiseaseOther,HereditaryDiseaseOther,CId ");
			strSql.Append(" FROM tb_UserInfor ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" UId,UserName,TrueName,IDNumber,PayPassword,Password,Gender,Age,PhoneNumberOne,PhoneNumberTwo,TelePhoneNumber,Email,MailActivation,QQ,QQTakenId,ResisterTime,HeadImgUrl,ContactAddress,IDAddress,GlyphPassword,GlyphPayPassword,IsDelete,DeleteTime,DeleteUserId,HaveFinance,Height,Weight,ChronicDisease,HereditaryDisease,ChronicDiseaseOther,HereditaryDiseaseOther,CId ");
			strSql.Append(" FROM tb_UserInfor ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tb_UserInfor ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.UId desc");
			}
			strSql.Append(")AS Row, T.*  from tb_UserInfor T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "tb_UserInfor";
			parameters[1].Value = "UId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

